Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC for const generics #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

RFC for const generics #42

wants to merge 5 commits into from

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Oct 27, 2024

README.md Outdated Show resolved Hide resolved
@xunilrj xunilrj force-pushed the xunilrj/const-generics branch from 9d50946 to de7209d Compare January 21, 2025 11:40
@xunilrj xunilrj self-assigned this Jan 21, 2025
@xunilrj xunilrj marked this pull request as ready for review January 21, 2025 11:41
@xunilrj xunilrj mentioned this pull request Jan 24, 2025
7 tasks
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the definition itself ends up pretty straightforward, the hard part is going to be the monomorphization implementation.

@IGI-111 IGI-111 requested a review from a team January 25, 2025 01:56
@JoshuaBatty JoshuaBatty requested a review from a team January 29, 2025 23:09

[motivation]: #motivation

Some types have constants, specifically unsigned integers, as their definition (e.g. arrays and string arrays). Without `const generics` it is impossible to have `impl` items for all instances of these types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Some types have constants, specifically unsigned integers, as their definition (e.g. arrays and string arrays). Without `const generics` it is impossible to have `impl` items for all instances of these types.
Some types have constants, specifically unsigned integers, as a part of their definition (e.g. arrays and string arrays). Without `const generics` it is impossible to have a single `impl` item for all instances of these types.

A simple example would be:

```rust
fn id<const SIZE: usize>(array: [u64; SIZE]) -> [u64; SIZE] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most of the code samples usize is used which is misleading because this type does not exist in Sway. Was the intention maybe to emphasize that any unsigned type can be used?

Even in that case, the proposal would be to go with u64 or other concrete types in examples and specify in the RFC which types can be used in const generics.

Or to mention on the top of the Guide-level explanation that usize means any of the types: u8, ...

Still, in this particular example even that is misleading because currently array size must be u64.

Suggested change
fn id<const SIZE: usize>(array: [u64; SIZE]) -> [u64; SIZE] {
fn id<const SIZE: u64>(array: [u64; SIZE]) -> [u64; SIZE] {

This also allows `impl` items such as

```rust
impl<const N: usize> AbiEncode for str[N] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl<const N: usize> AbiEncode for str[N] {
impl<const N: u64> AbiEncode for str[N] {


[reference-level-explanation]: #reference-level-explanation

This new syntax has three forms: declarations, instantiations and references.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This new syntax has three forms: declarations, instantiations and references.
This new syntax has three forms: declarations, instantiations, and references.


This new syntax has three forms: declarations, instantiations and references.

"const generics declarations" can appear anywhere all other generic arguments declarations are valid:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"const generics declarations" can appear anywhere all other generic arguments declarations are valid:
"Const generics declarations" can appear anywhere all other generic arguments declarations are valid:

```
6. Struct/enum support for const generics
7. Function/method declaration;
8. `impl` declarations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
8. `impl` declarations.
8. `impl` declarations;

6. Struct/enum support for const generics
7. Function/method declaration;
8. `impl` declarations.
9. Function/method reference;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
9. Function/method reference;
9. Function/method reference.


[prior-art]: #prior-art

This RFC is partially based on Rust's own const generic system: https://doc.rust-lang.org/reference/items/generics.html#const-generics, https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html, https://rust-lang.github.io/rfcs/2000-const-generics.html, https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This RFC is partially based on Rust's own const generic system: https://doc.rust-lang.org/reference/items/generics.html#const-generics, https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html, https://rust-lang.github.io/rfcs/2000-const-generics.html, https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html
This RFC is partially based on Rust's own const generic system:
- https://doc.rust-lang.org/reference/items/generics.html#const-generics
- https://blog.rust-lang.org/inside-rust/2021/09/06/Splitting-const-generics.html
- https://rust-lang.github.io/rfcs/2000-const-generics.html
- https://doc.rust-lang.org/beta/unstable-book/language-features/generic-const-exprs.html


[unresolved-questions]: #unresolved-questions

1. What is the impact of changing the "method called algorithm"?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. What is the impact of changing the "method called algorithm"?
1. What is the impact of changing the "method call search algorithm"?


1. What is the impact of changing the "method called algorithm"?

# Future possibilities
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above, implementing constraints like where N > 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants